/*
 * File:   EbookTest.cpp
 * Author: Luis Aguirre
 *
 * Created on 18-07-2013, 04:49:21 PM
 */

#include <list>

#include "ebooktest.h"
#include "ebook/ebook.h"

using namespace std;

CPPUNIT_TEST_SUITE_REGISTRATION(EBookTest);

EBookPage page;
const string dataPage = "History\n \
        The first theory about software was proposed by Alan Turing in his 1935 essay Computable \
        numbers with an application to the\n \
        Entscheidungsproblem (decision problem).[3] Colloquially, the term is often used \
        to mean application software. In computer science and\n\
        software 2engineering1, software is all information processed by computer system, \
        programs and data. The academic fields studying software are\n \
        computer science and software engineering.\n";
        
EBookTest::EBookTest()
{
}

EBookTest::~EBookTest()
{
}

void EBookTest::setUp()
{
    page.data = dataPage;
}

void EBookTest::tearDown()
{
}

void EBookTest::testTrimLine()
{
    string s1 = "   sample";
    trimLine(s1);
    string s2 = "sample   ";
    trimLine(s2);
    string s3 = "  sample ";
    trimLine(s3);
    string s4 = "  sample   l  ";
    trimLine(s4);
    
    string expected = "sample";
    string expected2 = "sample   l";
    
    CPPUNIT_ASSERT(s1 == expected);
    CPPUNIT_ASSERT(s2 == expected);
    CPPUNIT_ASSERT(s3 == expected);
    CPPUNIT_ASSERT(s4 == expected2);
}

void EBookTest::testReadEBook()
{
    const char* eBookPath = "resources/ebook.txt";
    const char* dicPath = "resources/dic.txt";
    EBook* book = new EBook(eBookPath, dicPath);
    CPPUNIT_ASSERT(book->getPages().size() == 6);
    list<EBookPage>::iterator pagesIt = book->getPages().begin();
    int count = 1;
    
    for (;  pagesIt != book->getPages().end(); pagesIt++)
    {
        stringstream ss;
        ss << "Hello" << count << endl;
        string result = pagesIt->getContent();
        string expected = ss.str();
        CPPUNIT_ASSERT(result.compare(expected) == 0);
        count++;
    }
}

void EBookTest::testReadEBookGivenANonExistentPath()
{
    try
    {
        const char* eBookPath = "resources/something.txt";
        const char* dicPath = "resources/dic.txt";
        EBook* book = new EBook(eBookPath, dicPath);
        book->printBook();
        CPPUNIT_ASSERT(false);
    }
    catch(FileException& e)
    {
        CPPUNIT_ASSERT(true);
    }
}

void EBookTest::testReadEBookGivenANull()
{
    try
    {
        EBook* book = new EBook(NULL, NULL);
        book->printBook();
        CPPUNIT_ASSERT(false);
    }
    catch(FileException& e)
    {
        CPPUNIT_ASSERT(true);
    }
}

void EBookTest::testContainsWordGivenAWord()
{
    
    CPPUNIT_ASSERT(page.containsWord("apPlIcation"));
}

void EBookTest::testContainsWordGivenAWordBetweenNumbers()
{
    CPPUNIT_ASSERT(page.containsWord("engineering"));
}

void EBookTest::testContainsWordGivenAWordInTheBeginOfTheLine()
{
    CPPUNIT_ASSERT(page.containsWord("programS"));
}

void EBookTest::testContainsWordGivenAWordInTheEndOfTheLine()
{
    CPPUNIT_ASSERT(page.containsWord("History"));
}

void EBookTest::testContainsWordGivenAWordBetweenTwoLines()
{
    CPPUNIT_ASSERT(page.containsWord("science and software"));
}

void EBookTest::testContainsWordGivenAWordInsideAnotherWord()
{
    CPPUNIT_ASSERT(!page.containsWord("study"));
}

void EBookTest::testContainsWordGivenANonExistentWord()
{
    CPPUNIT_ASSERT(!page.containsWord("Linux"));
}

void EBookTest::testGenerateBackIndexBook()
{
    EBook* book = new EBook("resources/ebook2.txt", "resources/dic.txt");
    BackBookIndex index = book->getBackBockIndex();
    index.write("resources/indexResult.txt");
    ifstream file("/home/luis/Documentos/ebook/test/resources/indexResult.txt");
    
    string expected = "Linux: 2\nMAIN: 3\nsoftWare: 1, 2, 3\nWindows: 2\nword: 2, 3\n";
    string buffer;
    string result;
    while(getline(file, buffer))
    {
        trimLine(buffer);
        result.append(buffer);
        result.append("\n");
    }
    file.close();
    
    CPPUNIT_ASSERT(result == expected);
}
